package com.aw.guiguzi.module.manager.transaction.adapter

import android.app.Activity
import android.view.View
import android.widget.EditText
import androidx.recyclerview.widget.RecyclerView
import com.aw.baselib.base.AwViewDialog
import com.aw.baselib.bean.CommonImgItemBean
import com.aw.baselib.util.AwEditTextListener
import com.aw.baselib.util.AwLog
import com.aw.baselib.util.AwRecyclerViewUtil
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.TransactionItemBean
import com.aw.guiguzi.util.AppCacheUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

class ManagerTransactionConfigAdapter :
    BaseQuickAdapter<TransactionItemBean, BaseViewHolder>(R.layout.adapter_manager_transaction_config) {
    private var mList: MutableList<TransactionItemBean>? = ArrayList()
    private var mUploadListener: OnUploadListener? = null

    init {
        if (mList == null) {
            mList = ArrayList()
        }
    }

    fun setUploadListener(uploadListener: OnUploadListener?) {
        mUploadListener = uploadListener
    }

    override fun convert(helper: BaseViewHolder, bean: TransactionItemBean) {
        helper.setText(R.id.et_title, bean.title)
            .setGone(R.id.tv_add, AppCacheUtil.isTransactionConfigAdd())
            .addOnClickListener(R.id.tv_add)
            .addOnClickListener(R.id.iv_delCategorize)
        val rcv_data = helper.getView<RecyclerView>(R.id.rcv_data)
        val subAdapter = ManagerTransactionConfigSubItemAdapter()
        AwRecyclerViewUtil.setRecyclerViewLinearlayout(
            mContext as Activity,
            rcv_data,
            subAdapter,
            false
        )
        subAdapter.addAllData(bean.list)
        subAdapter.setUploadListener (object : ManagerTransactionConfigSubItemAdapter.OnUploadListener{
            override fun toUpload(bean: CommonImgItemBean?) {
                mUploadListener!!.toUpload(bean, subAdapter)
            }

        })
        subAdapter.setOnDelListener(object : ManagerTransactionConfigSubItemAdapter.OnDelListener {
            override fun toDel(index: Int) {
                val dialog = AwViewDialog(mContext)
                dialog.showDialog("确定要删除吗？", true, { v: View? ->
                    bean.list.removeAt(index)
                    subAdapter.addAllData(bean.list)
                }) { _: View? -> dialog.dismiss() }
            }

        })
        AwEditTextListener.isChangeListener(helper.getView(R.id.et_title)) { changed: Boolean ->
            if (changed) {
                bean.title = (helper.getView<View>(R.id.et_title) as EditText).text.toString()
            }
        }
        var title = ""
        when (helper.layoutPosition) {
            0 -> title = "（" + "一）"
            1 -> title = "（" + "二）"
            2 -> title = "（" + "三）"
            3 -> title = "（" + "四）"
            4 -> title = "（" + "五）"
            5 -> title = "（" + "六）"
            else -> {}
        }
        helper.setText(R.id.tv_categorize, "分类 $title")
    }

    fun addAllData(dataList: MutableList<TransactionItemBean>?) {
        mList = dataList
        setNewData(mList)
        for (i in mList!!.indices) {
            val itemBean = mList!![i]
            AwLog.d("addAllData itemBean: $itemBean")
        }
//        notifyDataSetChanged()
    }

    fun clearData() {
        if (mList != null) {
            val startPosition = 0 //hasHeader is 1
            val preSize = mList!!.size
            if (preSize > 0) {
                mList!!.clear()
                notifyItemRangeRemoved(startPosition, preSize)
            }
        }
    }

    interface OnUploadListener {
        fun toUpload(bean: CommonImgItemBean?, subAdapter: ManagerTransactionConfigSubItemAdapter?)
    }
}